export default function (opt) {
  let $ = window.jquery
  console.log('request info', opt)
  $.ajax({
    method: opt.method || 'POST',
    url: opt.url,
    data: opt.params,
    timeout: opt.timeout || 5000,
    dataType: opt.responseType || 'json',
    xhrFields: {
      withCredentials: true
    },
    crossDomain: true
  })
  .done((res) => {
    console.log('request success', res)
    if (res.status === 200) {
      if (typeof opt.success === 'function') {
        opt.success(res)
      }
    } else {
      if (typeof opt.error === 'function') {
        opt.error(res)
      }
    }
  })
  .fail((err) => {
    console.warn('REQUEST FAIL', opt.url, err)
    if (err.status === 500) {
      if (typeof opt.error === 'function') {
        opt.error(JSON.parse(err.responseText))
      }
    } else {
      if (typeof opt.fail === 'function') {
        opt.fail(err)
      }
    }
  })
}
